/**
 * Copyright (c) 2009-2011, chunquedong(YangJiandong)
 * 
 * This file is part of ChunMap project
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
 * 
 * History:
 *     2010-05-05  Jed Young  Creation
 */
package chunmap.data.feature;

import java.util.List;

import chunmap.model.elem.Envelope;

public class FeatureUtil {
	public static String Feature2String(Shape f){
		StringBuilder s=new StringBuilder();
		FeatureCollection fc =f.getFeatureClass();

		s.append("<"+fc.getMetadata().getName()+">");
		
		s.append("<id>");
		s.append(f.getId());
		s.append("</id>");
		s.append("<geometry>");
		s.append(f.getGeometry().toString());
		s.append("</geometry>");
		
		FeatureSchama schama=fc.getFeatureSchama();
		int n=schama.size();
		for(int i=0;i<n;i++){
			String name=schama.getFieldName(i);
			String value=f.getVaule(i).toString();
			s.append("<"+name+">");
			s.append(value);
			s.append("</"+name+">");
		}
		
		s.append("</"+fc.getMetadata().getName()+">");
		
		return s.toString();
	}
	
	public static StringBuilder LayerHeadString(List<Feature> fs,Envelope env){
		StringBuilder s=new StringBuilder();
		
		s.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		s.append("<wfs:FeatureCollection xmlns=\"http://www.someserver.com/myns\"");
		s.append(" xmlns:myns=\"http://www.someserver.com/myns\"");
		s.append(" xmlns:wfs=\"http://www.opengis.net/wfs\"");
		s.append(" xmlns:gml=\"http://www.opengis.net/gml\"");
		s.append(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
		s.append(">");
		s.append("<gml:boundedBy>");
		s.append("<gml:Box>");
		s.append("  <gml:coord>");
		s.append("    <gml:X>"+env.getMinX()+"</gml:X>");
		s.append("    <gml:Y>"+env.getMinY()+"</gml:Y>");
		s.append("  </gml:coord>");
		s.append("  <gml:coord>");
		s.append("    <gml:X>"+env.getMaxX()+"</gml:X>");
		s.append("    <gml:Y>"+env.getMaxY()+"</gml:Y>");
		s.append("  </gml:coord>");
		s.append("</gml:Box>");
		s.append("</gml:boundedBy>");
		
		s.append("<gml:featureMember>");
		for(Feature f:fs){
			s.append(f.toString());
		}
		s.append("</gml:featureMember>");
		s.append("</wfs:FeatureCollection>");
		return s;
	}
}